<?php
defined('_JEXEC') or die('Restricted access');

require_once(JPATH_COMPONENT.DS.'persistance'.DS.'DAOQa.php');

class DAOQaImpl implements DAOQa{
	
	public function updateQa($con, $dtoQa){
		$sql = "UPDATE #__hizkuntzak_qas SET q_es='$dtoQa->q_es', ";
		$sql = $sql."q_eu='$dtoQa->q_eu', ";
		$sql = $sql."a_eu='$dtoQa->a_eu', a_es='$dtoQa->a_es', ";
		$sql = $sql."active=$dtoQa->active WHERE id=$dtoQa->id ";
		
		$con->setQuery($sql);
		
		if(!$con->query()){
			$con->stderr();
			return false;
		}
		else
			return true;
	}
	
	public function getQa($con, $id){
		$sql = "select * from #__hizkuntzak_qas where (id=$id)";
		
		$con->setQuery($sql);
		
    	$row = $con->loadObject();
    	
    	if($row == null){
    		return null;
    	}
    	
    	$qa = new DTOQa();
        	
        $qa->setId($row->id);
        $qa->setQEs($row->q_es);
        $qa->setQEu($row->q_eu); 
        $qa->setAEs($row->a_es);
        $qa->setAEu($row->a_eu); 
        $qa->setActive($row->active);

        return $qa;
	}
	
	public function getQas($con, $active){
		$results = array();
		
		$sql = "select * from #__hizkuntzak_qas ";
		
		if($active){
			$sql = $sql. "where (active=1)";
		}
		
		$con->setQuery($sql);
		
    	$objectList = $con->loadObjectList();

        foreach($objectList as $row) {

        	$qa = new DTOQa();
        	
        	$qa->setId($row->id);
        	$qa->setQEs($row->q_es);
        	$qa->setQEu($row->q_eu); 
        	$qa->setAEs($row->a_es);
        	$qa->setAEu($row->a_eu); 
        	$qa->setActive($row->active);
            
        	$results[] = $qa;
          } 
		
		return $results;
	}
	
	public function addQa($conn, $dtoQa){
		
		$data = new stdClass();
		$data->id = null;
		$data->q_es = $dtoQa->getQEs();
		$data->q_eu = $dtoQa->getQEu();
		$data->a_es = $dtoQa->getAEs();
		$data->a_eu = $dtoQa->getAEu();
		$data->active = $dtoQa->isActive();
		
		if(!$conn->insertObject( '#__hizkuntzak_qas', $data, 'id' )){
			echo $conn->stderr();
			return false;
		}
		else 
			return true;
	}

}